﻿Public Class Map

    Dim DashedPen As New Pen(Brushes.Green) With {.DashStyle = Drawing2D.DashStyle.Dot}

    Sub New()
        InitializeComponent()
        ToolBar.Parent = Nothing
        ToolBar.Renderer = New FirefoxRenderer
        BtnHelp.DropDown = HelpMenu
        MnuOpen.Image = My.Resources.personel.Resize(16, 16)
        MnuRefresh.Image = My.Resources.refresh.Resize(16, 16)
        BtnHelp.Image = BtnHelp.Image.Resize(16, 16)
        Grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
        Grid.RowHeadersVisible = False
        Grid.AddColumn("Personel", "Personel", "150", "l")
        Grid.Columns(0).Frozen = True
        Grid.Rows.Add(3)
        Grid.Rows(0).Frozen = True
        Grid.Rows(1).Frozen = True
        Grid.Rows(2).Frozen = True
        Grid.Rows(0).DefaultCellStyle.Font = New Font("segoe ui", 6, FontStyle.Bold)
        Grid.Rows(1).DefaultCellStyle.Font = New Font("segoe ui", 6, FontStyle.Bold)
        Grid.Rows(2).DefaultCellStyle.Font = New Font("segoe ui", 6, FontStyle.Bold)
        Grid.CellBorderStyle = DataGridViewCellBorderStyle.None
        Grid.AllowUserToResizeColumns = False
        Grid.AllowUserToResizeRows = False
        Grid.ColumnHeadersVisible = False
        Grid.AllowUserToAddRows = False
        Dim i As Integer = 1
        For a = 1 To 12
            Dim d As New Date(2011, a, 1)
            Dim Ad As String = Choose(a, "OCAK", "ŞUBAT", "MART", "NİSAN", "MAYIS", "HAZİRAN", "TEMMUZ", "AĞUSTOS", "EYLÜL", "EKİM", "KASIM", "ARALIK")
            Ad = StrDup(3, " ") & Ad
            Do Until d.Month <> a
                Grid.AddColumn("d-" & a & "-" & d.Day, d.Day, "13", "c")
                Grid.Columns(i).Tag = d
                Grid.Rows(0).Cells(i).Value = Mid(Ad, 1 + ((d.Day - 1) Mod Len(Ad)), 1)
                d = d.AddDays(1)
                i += 1
            Loop
        Next
        Reload()
    End Sub

    Private Sub Grid_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles Grid.CellPainting
        If e.ColumnIndex = 0 Then
            e.Handled = True
            Dim F As New StringFormat
            F.Alignment = StringAlignment.Near
            F.LineAlignment = StringAlignment.Center
            Dim R = e.CellBounds
            e.Graphics.FillRectangle(Brushes.LightBlue, R)
            R.Width -= 1
            R.Height -= 1
            e.Graphics.DrawLine(Pens.Black, R.Right, R.Top, R.Right, R.Bottom)
            e.Graphics.DrawString(e.FormattedValue, e.CellStyle.Font, Brushes.Black, R, F)
        Else
            e.Handled = True
            Dim d As Date = Grid.Columns(e.ColumnIndex).Tag
            Dim day = "P.Pt.S.Ç.Pr.C.Ct".Split(".")(d.DayOfWeek)
            Dim F As New StringFormat
            F.Alignment = StringAlignment.Center
            F.LineAlignment = StringAlignment.Center
            Dim R = e.CellBounds
            If Tatil(d) Then
                e.Graphics.FillRectangle(New SolidBrush(Color.LightGray.Lighter(0.5)), R)
            Else
                e.Graphics.FillRectangle(Brushes.White, R)
            End If
            R.Width -= 1
            R.Height -= 1
            If day = "P" Then
                e.Graphics.DrawLine(Pens.Black, R.Right, R.Top, R.Right, R.Bottom)
            Else
                e.Graphics.DrawLine(Pens.LightGray, R.Right, R.Top, R.Right, R.Bottom)
            End If
            e.Graphics.DrawLine(Pens.LightGray, R.Left, R.Bottom, R.Right - 1, R.Bottom)
            If e.RowIndex = 2 Then
                Dim Col = Color.Black
                If Tatil(d) Then Col = Color.Red
                e.Graphics.DrawLine(Pens.Black, R.Left, R.Bottom, R.Right, R.Bottom)
                e.Graphics.DrawString(day, e.CellStyle.Font, New SolidBrush(Col), R, F)
                If d.Month <> d.AddDays(1).Month Then e.Graphics.DrawLine(DashedPen, R.Right, R.Top, R.Right, R.Bottom)
            ElseIf e.RowIndex = 1 Then
                Dim Col = Color.Black
                If Tatil(d) Then Col = Color.Red
                If day <> "P" Then R.Width += 1
                'e.Graphics.FillRectangle(Brushes.White, R)
                If day <> "P" Then R.Width -= 1
                e.Graphics.DrawLine(Pens.Black, R.Left, R.Bottom, R.Right, R.Bottom)
                If d.Month <> d.AddDays(1).Month Then e.Graphics.DrawLine(DashedPen, R.Right, R.Top, R.Right, R.Bottom)
                R.Inflate(1, 0)
                e.Graphics.DrawString(d.Day, e.CellStyle.Font, New SolidBrush(Col), R, F)
            ElseIf e.RowIndex = 0 Then
                R.Width += 1
                e.Graphics.FillRectangle(Brushes.White, R)
                R.Width -= 1
                If d.Month <> d.AddDays(1).Month Then e.Graphics.DrawLine(DashedPen, R.Right, R.Top, R.Right, R.Bottom)
                e.Graphics.DrawLine(Pens.Black, R.Left, R.Bottom, R.Right, R.Bottom)
                Dim n = " " & Choose(d.Month, "OCAK", "ŞUBAT", "MART", "NİSAN", "MAYIS", "HAZİRAN", "TEMMUZ", "AĞUSTOS", "EYLÜL", "EKİM", "KASIM", "ARALIK") & " " & d.Year
                e.Graphics.DrawString(Mid(n, d.Day, 1), e.CellStyle.Font, Brushes.Green, R, F)
            Else
                If d.Month <> d.AddDays(1).Month Then e.Graphics.DrawLine(DashedPen, R.Right, R.Top, R.Right, R.Bottom)
                R.Inflate(0, -3)
                R.Width += 1
                If InStr(e.FormattedValue, "<") > 0 Then R.Offset(3, 0) : R.Width -= 3
                If InStr(e.FormattedValue, ">") > 0 Then R.Width -= 4
                Select Case Mid(e.FormattedValue, 1, 1)
                    Case "i"
                        e.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(128, Color.Blue)), R)
                    Case "h"
                        e.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(128, Color.Red)), R)
                    Case "m"
                        e.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(128, Color.Orange)), R)
                End Select
            End If
        End If
        If (e.State And DataGridViewElementStates.Selected) > 0 Then
            Dim R = e.CellBounds
            e.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(64, SystemColors.Highlight)), R)
        End If
    End Sub

    Private Sub Grid_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Grid.DoubleClick
        MnuOpen.PerformClick()
    End Sub

    Private Sub Reload()
        Service.Log("map", 0, Kullanici.Kod)
        For i = Grid.Rows.Count - 1 To 3 Step -1
            Grid.Rows.RemoveAt(i)
        Next
        Grid.Refresh()
        For Each P In Service.Personel.Liste(Kullanici.TBirim)
            Grid.Rows.Add(P.Ad)
            Dim Row = Grid.Rows(Grid.RowCount - 1)
            Row.Tag = P.Kod
            For Each h In Service.Izin.Liste(P.Kod, 2011)
                Dim d = h.Tarih1
                Do Until d > h.Tarih2
                    If TypeOf h Is HareketYillikIzin Then
                        Row.Cells("d-" & d.Month & "-" & d.Day).Value = "i"
                    ElseIf TypeOf h Is HareketHastalikIzni Then
                        Row.Cells("d-" & d.Month & "-" & d.Day).Value = "h"
                    ElseIf TypeOf h Is HareketMazaretIzni Then
                        Row.Cells("d-" & d.Month & "-" & d.Day).Value = "m"
                    End If
                    If d = h.Tarih1 Then Row.Cells("d-" & d.Month & "-" & d.Day).Value &= "<"
                    If d = h.Tarih2 Then Row.Cells("d-" & d.Month & "-" & d.Day).Value &= ">"
                    d = d.AddDays(1)
                Loop
            Next
        Next
    End Sub

    Private Sub MnuOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuOpen.Click
        If Grid.SelectedRows.Count <> 1 Then Exit Sub
        If Grid.SelectedRows(0).Index < 3 Then Exit Sub
        CType(Parent, FrmMain).Open(Grid.SelectedRows(0).Tag)
    End Sub

    Private Sub MnuRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuRefresh.Click
        Reload()
    End Sub

End Class
